package com.ecoolex.ying.space.api.graphql;

import java.util.Date;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.ying.space.entity.UserLikeRecord;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.UserWorksComment;
import com.ecoolex.ying.space.pojo.AddUserLikeRecordDTO;
import com.ecoolex.ying.space.pojo.UserLikeAndFavoriteDTO;
import com.ecoolex.ying.space.ws.UserLikeRecordService;
import com.ecoolex.ying.space.ws.UserWorksCommentService;
import com.ecoolex.ying.space.ws.UserWorksService;


@GraphQL2Module(type = Type.User, description = "用户点赞管理")
public class UserLikeRecordGraphql {

    @Reference
    private UserLikeRecordService userLikeRecordService;

    @Autowired
    private ConsumerMemberManager consumerMemberManager;

    @Autowired
    private UserWorksService userWorksService;

    @Autowired
    private UserWorksCommentService userWorksCommentService;

    @TypeField(type = UserLikeAndFavoriteDTO.class, field = "userInfo", description = "用户信息")
    public ConsumerMember getCommentUser(@Source UserLikeAndFavoriteDTO source) {
        return consumerMemberManager.getByUserId(source.getUserId());
    }

    @TypeField(type = UserLikeAndFavoriteDTO.class, field = "userWorks", description = "用户作品")
    public UserWorks userWorks(@Source UserLikeAndFavoriteDTO source) {
        return userWorksService.getUserWorksByIdForH5(source.getContentId());
    }

    @TypeField(type = UserLikeAndFavoriteDTO.class, field = "userWorksComment", description = "用户评论")
    public UserWorksComment userWorksComment(@Source UserLikeAndFavoriteDTO source) {
        return userWorksCommentService.getByIdForH5(source.getContentId());
    }

    @QueryField(field = "myUserLikeCount", description = "我的获赞数量")
    public Integer myUserLikeCount(ConsumerMemberAuthority user) {
        user.assertLogin();
        return userLikeRecordService.userLikeCountByContentAuthorUserId(user.userId());
    }

    @QueryField(field = "otherUserLikeCount", description = "他人获赞数量")
    public Integer otherUserLikeCount(@NonNull Integer userId) {
        return userLikeRecordService.userLikeCountByContentAuthorUserId(userId);
    }

    @QueryField(field = "receivedUserLikeRecordAndFavoriteList", description = "我收到的点赞和收藏列表")
    public Pagination<UserLikeAndFavoriteDTO> queryByContentAuthorUserId(@NonNull Date now, @NonNull Sorting[] sorts, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return userLikeRecordService.queryByContentAuthorUserId(user.userId(), now, sorts, page);
    }

    @QueryField(field = "unreadReceivedUserLikeAndFavoriteCount", description = "我收到的点赞和收藏未读数量")
    public long unreadReceivedUserLikeAndFavoriteCount(ConsumerMemberAuthority user) {
        if (user.isLogin()) {
            return userLikeRecordService.unreadReceivedUserLikeAndFavoriteCount(user.userId());
        } else {
            return 0;
        }
    }

    @MutationField(field = "addUserLikeRecord", description = "点赞")
    public boolean add(@NonNull AddUserLikeRecordDTO add, ConsumerMemberAuthority user) {
        user.assertLogin();
        return userLikeRecordService.add(add, user.userId(), user.userName());
    }

    @MutationField(field = "cancelUserLikeRecord", description = "取消点赞")
    public boolean cancel(@NonNull Integer type, @NonNull Integer contentId, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserLikeRecord userLikeRecord
            = userLikeRecordService.getByUserIdAndTypeAndContentId(user.userId(), type, contentId);
        BizAssert.isTrue(Check.notNull(userLikeRecord), ResultCode.VERIFY.build(1), "点赞记录不存在");
        return userLikeRecordService.delete(userLikeRecord, user.userName());
    }

    @MutationField(field = "updateReceivedUserLikeAndFavoriteReadState", description = "消息中心-更新收到的赞和收藏读取状态")
    public boolean updateReceivedUserLikeAndFavoriteReadState(ConsumerMemberAuthority user) {
        user.assertLogin();
        userLikeRecordService.updateReceivedUserLikeAndFavoriteReadState(user.userId(), user.userName());
        return true;
    }
}
